using Crossborder.Domian.Orders;
using Crossborder.Domian.Products;
using Crossborder.Domian.Users;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Crossborder.Core
{
    public class CrossborderDbContext : DbContext
    {
        public CrossborderDbContext()
        {
            
        }

        public CrossborderDbContext(DbContextOptions<CrossborderDbContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region 用户
            modelBuilder.Entity<User>(p =>
            {
                // p.Property(e => e.Id).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
                p.Property(e => e.UserId).HasComment("用户Id");
                p.Property(e => e.UserName).HasComment("用户名");
                p.Property(e => e.NickName).HasComment("昵称");
                p.Property(e => e.Role).HasComment("角色");
                p.Property(e => e.Email).HasComment("邮箱");
            });
            modelBuilder.Entity<UserPassword>(p => { });
            #endregion

            #region 产品
            modelBuilder.Entity<Categorys>(p => { });
            modelBuilder.Entity<ProductInfos>(p => { });
            modelBuilder.Entity<ProductClassify>(p =>
            {
                p.Property(e => e.Price).HasPrecision(18, 3);
            });
            modelBuilder.Entity<ProductPhotos>(p => { });
            #endregion

            #region 订单
            modelBuilder.Entity<OrderMaster>(p => { });
            modelBuilder.Entity<OrderDetail>(p =>
            {
                p.Property(e => e.Price).HasPrecision(18, 3);
            });
            #endregion

            base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                // optionsBuilder.UseSqlServer(_connectionString);
                optionsBuilder.UseSqlServer("Server=localhost;Database=CrossborderDb;uid=Lemon;pwd=lemon2501.;TrustServerCertificate=true;");
            }
            base.OnConfiguring(optionsBuilder);
        }
    }
}